#include "test.h"
#include "namespace.h"

static int
test_namespace()
{
    LSUP_NSMap *nsm = LSUP_nsmap_new();
    ASSERT (nsm != NULL, "Error creating namespace map!");

    EXPECT_PASS (
            LSUP_nsmap_add (nsm, "dc", "http://purl.org/dc/elements/1.1/"));
    EXPECT_PASS (LSUP_nsmap_add (nsm, "dcterms", "http://purl.org/dc/terms/"));

    // Look up prefix.
    EXPECT_STR_EQ (
            LSUP_nsmap_get_ns (nsm, "dc"), "http://purl.org/dc/elements/1.1/");
    EXPECT_STR_EQ (
            LSUP_nsmap_get_ns (nsm, "dcterms"), "http://purl.org/dc/terms/");
    // Prefixes longer than 7 chars are truncated.
    ASSERT (LSUP_nsmap_get_ns (nsm, "dctermsxx"), "http://purl.org/dc/terms/");
    ASSERT (LSUP_nsmap_get_ns (nsm, "none") == NULL, "Non-existent NS found!");

    // Lookup NS.
    EXPECT_STR_EQ (
            LSUP_nsmap_get_pfx (nsm, "http://purl.org/dc/elements/1.1/"), "dc");
    EXPECT_STR_EQ (
            LSUP_nsmap_get_pfx (nsm, "http://purl.org/dc/terms/"), "dcterms");

    // Normalize and denormalize URIs.
    char *fq_uri, *pfx_uri;
    fq_uri = "http://purl.org/dc/elements/1.1/title";

    EXPECT_PASS (LSUP_nsmap_denormalize_uri (nsm, fq_uri, &pfx_uri));
    EXPECT_STR_EQ (pfx_uri, "dc:title");

    fq_uri = NULL;
    EXPECT_PASS (LSUP_nsmap_normalize_uri (nsm, pfx_uri, &fq_uri));
    EXPECT_STR_EQ (fq_uri, "http://purl.org/dc/elements/1.1/title");

    EXPECT_PASS (LSUP_nsmap_remove (nsm, "dc"));
    ASSERT (
            LSUP_nsmap_remove (nsm, "none") == LSUP_NOACTION,
            "Wrong result for removal of non-existent prefix!");
    ASSERT (LSUP_nsmap_get_ns (nsm, "dc") == NULL, "Deleted NS found!");

    LSUP_nsmap_free (nsm);
    free (fq_uri);
    free (pfx_uri);

    return 0;
}

int namespace_tests()
{
    RUN (test_namespace);
    return 0;
}
